﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using ConsolidationTool.Common.Entities;
using ConsolidationTool.Common;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
using ConsolidationTool.WebParts.ControlTemplates;

namespace ConsolidationTool.WebParts.Flash_Input.Flash_Input_SCS.SCSNewForm
{
    public partial class SCSNewFormUserControl : FSUserControlBase
    {
        List<Group> regionGroup = new List<Group>();
        List<Group> subRegionGroup = new List<Group>();
        List<Group> entityGroup = new List<Group>();

        protected void Page_Load(object sender, EventArgs e)
        {
            TopSelector = (commonEntitySelector as CommonEntitySelector);
            TopSelector.EntitySelectorChanged += new CommonEntitySelector.EntitySelectorChangeHandler(TopSelector_EntitySelectorChanged);
            (commonButtomAction as CommonButtomAction).SaveButtonClicked += new CommonButtomAction.SaveButtonClickHandler(ESNewFormUserControl_SaveButtonClicked);

            if (!IsPostBack)
            {
                
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (!IsPostBack)
                base.FormInitialization();
            base.OnPreRender(e);
        }

        public override SPListItem Item
        {
            get
            {
                return ConsolidationTool_CoName.ListItem;
            }
        }

        void ESNewFormUserControl_SaveButtonClicked(object sender, EventArgs e)
        {
            this.Page.Validate();
            base.Save();
        }
        /// <summary>
        /// 给YTD-1负值
        /// </summary>
        void SetValueToYTDLastMonth()
        {
            LabelSV01030100.Text = GetYTDLastMonthValue("SV01030100");
            LabelSV01030101.Text = GetYTDLastMonthValue("SV01030101");
            LabelSV01030102.Text = GetYTDLastMonthValue("SV01030102");
            LabelSV01030103.Text = GetYTDLastMonthValue("SV01030103");
            //LabelSV01030104.Text = GetYTDLastMonthValue("SV01030104");
            LabelSV01040100.Text = GetYTDLastMonthValue("SV01040100");
            LabelSV01050100.Text = GetYTDLastMonthValue("SV01050100");
            LabelSV01050101.Text = GetYTDLastMonthValue("SV01050101");
            LabelSV01050102.Text = GetYTDLastMonthValue("SV01050102");
            LabelSV01050103.Text = GetYTDLastMonthValue("SV01050103");
            LabelSV01060100.Text = GetYTDLastMonthValue("SV01060100");
        }
        

        void TopSelector_EntitySelectorChanged(object sender, int EntityID)
        {
            
            SetValueToYTDLastMonth();
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (TopSelector.drpEntity.SelectedIndex == 0)
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "jsError", "<script>alert('Entity is null')</script>");
                return;
            }
            this.Page.Validate();
            if (Page.IsValid)
            {
                SPListItem item = ConsolidationTool_CoCd.ListItem;

                item["CoCd"] = TopSelector.lbCoCd.Text;
                item["CoFullName"] = TopSelector.lbCompanyFullName.Text;
                item["CoName"] = TopSelector.drpEntity.SelectedItem.Text;
                item["Year"] = TopSelector.yearTextBox.Text;
                item["Month"] = TopSelector.monthTextBox.Text;
                item["Region"] = TopSelector.drpRegion.SelectedItem.Text;
                item["SubRegion"] = TopSelector.drpSubRegion.SelectedItem.Text;

                Title.ItemFieldValue = TopSelector.lbCoCd.Text + "_" + TopSelector.yearTextBox.Text + TopSelector.monthTextBox.Text;
                SaveButton.SaveItem(SPContext.Current, false, string.Empty);
                Utilities.CloseForm();
            }
        }
        protected void btnExit_Click(object sender, EventArgs e)
        {
            Utilities.CloseForm();
        }
        /// <summary>
        /// 根据字段名称获取YTD-1的和
        /// </summary>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        string  GetYTDLastMonthValue(string fieldName)
        {

            string rst = "0";

            DataTable dt = Common.BusinessService.GetSCSYTDLastMonthData(TopSelector.yearTextBox.Text, TopSelector.monthTextBox.Text, TopSelector.drpEntity.SelectedValue);
            if (dt != null)
                rst = dt.Compute("SUM("+fieldName+")", "").ToString();
            if(rst == null || rst == "")
            {
                rst = "0";
            }
            return rst;
        }
    }
}
